<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="keywords"
      ></el-input>
      <el-button @click="clearKeyWord">清除</el-button>
      <el-button type="primary" @click="serarch">搜索</el-button>
    </div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="eidtList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delList(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        :page-size="params._limit"
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      width="400px"
    >
      <el-form
        :model="formcontent"
        label-width="80px"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="formcontent.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="formcontent.des" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListApi,
  addListApi,
  editListApi,
  updateListApi,
  delListApi,
  searchApi,
} from "@/api/list.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      list: [],
      total: 0,
      dialogVisible: false,
      formcontent: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [{ required: true, message: "任务名称必填", trigger: "blur" }],
        des: [{ required: true, message: "任务描述必填", trigger: "blur" }],
      },
      keywords: "",
    };
  },
  methods: {
    async getList() {
      const res = await getListApi(this.params);
      // console.log(res);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
      // console.log(this.total);
    },
    handleCurrentChange(page) {
      this.params._page = page;
      this.getList();
    },
    openDialog() {
      this.dialogVisible = true;
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.formcontent.id = "";
    },
    addList() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.formcontent.id) {
            const res = await updateListApi(this.formcontent);
            // console.log(res);
            this.handleClose();
            this.getList();
            if (res.status === 200) {
              this.$message.success("更新成功");
            } else {
              this.$message.error("更新失败");
            }
          } else {
            this.formcontent.id = uuidv4();
            const res = await addListApi(this.formcontent);
            // console.log(res);
            this.handleClose();
            this.getList();
            if (res.status === 201) {
              this.$message.success("添加成功");
            } else {
              this.$message.error("添加失败");
            }
          }
        }
      });
    },
    async eidtList(id) {
      // console.log(id);
      this.dialogVisible = true;
      const res = await editListApi(id);
      // console.log(res);
      this.formcontent = res.data;
    },
    async delList(id) {
      const res = await delListApi(id);
      console.log(res);
      this.getList();
      if (res.status === 200) {
        this.$message.success("删除成功");
      } else {
        this.$message.error("删除失败");
      }
    },
    clearKeyWord() {
      this.keywords = "";
      this.getList();
    },
    async serarch() {
      const res = await searchApi(this.keywords);
      console.log(res);
      this.list = res.data;
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
